﻿using Kingdee.BOS.Orm.DataEntity;
using SD.WRZS.K3.ServiceFacade.KDServiceEntity.Mobile;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SDWXServer.Models
{
    /// <summary>
    /// 销售订单
    /// </summary>
    [Serializable]
    public class WXSaleOrder : AbstractBillObject
    {
        public WXSaleOrder()
        {
            this.DocumentStatus = "Z";
            this.CancelStatus = "A";
            this.Creator = new User();

            Organization = new Organization();
            Material = new Material();
            DocumentStatusName = "";
            DeliveryDate = DateTime.Now.Date;
            Customer = new Customer();
            HT = "";
        }

        /// <summary>
        /// 业务对象标识
        /// </summary>
        public string FormId { get; set; }
        public string BillTypeId { get; set; }
        public string BillTypeName { get; set; }
        /// <summary>
        /// 单据内码
        /// </summary>
        public string Id { get; set; }

        public Int64 BillId { get; set; }
        public string BillNo { get; set; }

        public DateTime BillDate { get; set; }

        public string DocumentStatus { get; set; }
        public string CancelStatus { get; set; }
        public string CloseStatus { get; set; }
        public bool IsSuspend { get; set; }

        /// <summary>
        /// 制单人
        /// </summary>
        public User Creator { get; set; }

        public Int64 ScanLogId { get; set; }

        /// <summary>
        /// 本地操作完成
        /// </summary>
        public bool IsLocalOperationFinished { get; set; }
        /// <summary>
        /// 数据已上传
        /// </summary>
        public bool IsUpload { get; set; }
        public string UploadMessage { get; set; }
        public DateTime? UploadTime { get; set; }

        /// <summary>
        /// 分录内码
        /// </summary>
        public Int64 EntryId { get; set; }
        public Int32 Seq { get; set; }

        /// <summary>
        /// 单据状态名称
        /// </summary>
        public string DocumentStatusName { get; set; }
        /// <summary>
        /// 提货地点
        /// </summary>
        public Organization Organization { get; set; }
        /// <summary>
        /// 物料
        /// </summary>
        public Material Material { get; set; }
        /// <summary>
        /// 计划数量
        /// </summary>
        public decimal PlanQty { get; set; }
        /// <summary>
        /// 出库数量
        /// </summary>
        public decimal StockQty { get; set; }
        /// <summary>
        /// 累计提货数量
        /// </summary>
        public decimal DeliQty { get; set; }
        /// <summary>
        /// 剩余提货数量
        /// </summary>
        public decimal CanOutQty { get; set; }
        /// <summary>
        /// 含税单价
        /// </summary>
        public decimal TaxPrice { get; set; }
        /// <summary>
        /// 价税合计
        /// </summary>
        public decimal AllAmount { get; set; }

        /// <summary>
        /// 交货日期
        /// </summary>
        public DateTime DeliveryDate { get; set; }
        /// <summary>
        /// 客户
        /// </summary>
        public Customer Customer { get; set; }
        /// <summary>
        /// 合同编号
        /// </summary>
        public string HT { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string Note { get; set; }
        public static List<WXSaleOrder> Parse(DynamicObject obj)
        {
            var orders = new List<WXSaleOrder>();

            var entryRows = obj["SaleOrderEntry"] as DynamicObjectCollection;
            foreach (var row in entryRows)
            {
                var item = new WXSaleOrder();
                orders.Add(item);
                item.BillId = Convert.ToInt64(obj["Id"]);
                item.BillNo = Convert.ToString(obj["BillNo"]);
                item.BillDate = Convert.ToDateTime(obj["Date"]);
                item.BillTypeId = Convert.ToString(obj["BillTypeId_Id"]);
                item.BillTypeName = Convert.ToString((obj["BillTypeId"] as DynamicObject)["Name"]);
                item.DocumentStatus = Convert.ToString(obj["DocumentStatus"]);
                item.CloseStatus = Convert.ToString(obj["CloseStatus"]);
                switch (item.DocumentStatus)
                {
                    case "Z":
                        item.DocumentStatusName = "暂存";
                        break;
                    case "A":
                        item.DocumentStatusName = "创建";
                        break;
                    case "B":
                        item.DocumentStatusName = "审核中";
                        break;
                    case "C":
                        item.DocumentStatusName = "已审核";
                        break;
                    case "D":
                        item.DocumentStatusName = "重新审核";
                        break;
                }
                if (item.CloseStatus == "B")
                {
                    item.DocumentStatusName = "已关闭";
                }
                item.HT = Convert.ToString(obj["FHT_SD"]);
                item.Note = Convert.ToString(obj["Note"]);

                var orgObj = obj["SaleOrgId"] as DynamicObject;
                item.Organization.Id = Convert.ToInt64(orgObj["Id"]);
                item.Organization.Number = Convert.ToString(orgObj["Number"]);
                item.Organization.Name = Convert.ToString(orgObj["Name"]);

                var cust = obj["CustId"] as DynamicObject;
                item.Customer.Id = Convert.ToInt64(cust["Id"]);
                item.Customer.MasterId = Convert.ToInt64(cust["msterID"]);
                item.Customer.Number = Convert.ToString(cust["Number"]);
                item.Customer.Name = Convert.ToString(cust["Name"]);

                item.EntryId = Convert.ToInt64(row["Id"]);

                var material = row["MaterialId"] as DynamicObject;
                item.Material.Id = Convert.ToInt64(material["Id"]);
                item.Material.MasterId = Convert.ToInt64(material["msterID"]);
                item.Material.Number = Convert.ToString(material["Number"]);
                item.Material.Name = Convert.ToString(material["Name"]);
                item.Material.Specification = Convert.ToString(material["Specification"]);

                var unit = row["UnitId"] as DynamicObject;
                item.Material.StockUnit.Id = Convert.ToInt64(unit["Id"]);
                item.Material.StockUnit.Number = Convert.ToString(unit["Number"]);
                item.Material.StockUnit.Name = Convert.ToString(unit["Name"]);

                item.Material.SaleUnit.Id = item.Material.StockUnit.Id;
                item.Material.SaleUnit.Number = item.Material.StockUnit.Number;
                item.Material.SaleUnit.Name = item.Material.StockUnit.Name;

                item.Material.PurchaseUnit.Id = item.Material.StockUnit.Id;
                item.Material.PurchaseUnit.Number = item.Material.StockUnit.Number;
                item.Material.PurchaseUnit.Name = item.Material.StockUnit.Name;

                item.PlanQty = Convert.ToDecimal(row["Qty"]);
                item.StockQty = Convert.ToDecimal(row["StockOutQty"]);
                item.DeliQty = Convert.ToDecimal(row["DeliQty"]);
                item.CanOutQty = Convert.ToDecimal(row["CanOutQty"]);
                item.DeliveryDate = Convert.ToDateTime(row["DeliveryDate"]);
                item.TaxPrice = Convert.ToDecimal(row["TaxPrice"]);
                item.AllAmount = Convert.ToDecimal(row["AllAmount"]);

                item.DeliQty = item.PlanQty - item.CanOutQty;

                item.Id = string.Format("{0}_{1}", item.BillId, item.EntryId);
            }

            return orders;
        }
    }
}